﻿using System.IO;
using System.Text;
using VideoTools.SubtitleEdit.Common;
using VideoTools.SubtitleEdit.VobSub;

namespace VideoTools.service
{
    /// <summary>
    /// sub字幕解析
    /// </summary>
    internal class SubImageSubtitleService : IImageSubtitleService
    {
        private readonly string filePath;
        private StringBuilder log;
        //sub
        private List<VobSubMergedPack> vobList;
        List<Color> _palette;

        public SubImageSubtitleService(string filePath)
        {
            this.filePath = filePath;
            log = new StringBuilder();
        }
        public int GetSubtitleCount()
        {
            return this.vobList.Count;
        }
        public SubtitleEncode parseSubtitle()
        {

            string idxFileName = Path.ChangeExtension(filePath, ".idx");
            var vobSubParser = new VobSubParser(true);
            vobSubParser.OpenSubIdx(filePath, idxFileName);
            List<VobSubMergedPack> _vobSubMergedPackList = vobSubParser.MergeVobSubPacks();
            _palette = vobSubParser.IdxPalette;
            vobSubParser.VobSubPacks.Clear();
            SubtitleEncode _subtitle = null;

            var languageStreamIds = new List<int>();
            foreach (var pack in _vobSubMergedPackList)
            {
                if (pack.SubPicture.Delay.TotalMilliseconds > 500 && !languageStreamIds.Contains(pack.StreamId))
                {
                    languageStreamIds.Add(pack.StreamId);
                }
            }
            int chosenStreamId = -1;
            if (languageStreamIds.Count > 1)
            {
                for (int i = 0; i < vobSubParser.IdxLanguages.Count; i++)
                {
                    string languageName = vobSubParser.IdxLanguages[i];
                    if (languageName.Contains("汉语"))
                    {
                        chosenStreamId = languageStreamIds[i];
                    }
                }

                if (chosenStreamId == -1)
                {
                    chosenStreamId = languageStreamIds[0];
                }
            }
            else
            {
                chosenStreamId = languageStreamIds[0];
            }

            _subtitle = new SubtitleEncode();
            vobList = new List<VobSubMergedPack>();
            for (int i = 0; i < _vobSubMergedPackList.Count; i++)
            {
                var x = _vobSubMergedPackList[i];
                if (x.StreamId == chosenStreamId)
                {
                    vobList.Add(x);
                    Paragraph p = new Paragraph(string.Empty, x.StartTime.TotalMilliseconds, x.EndTime.TotalMilliseconds);
                    if (x.IdxLine != null)
                    {
                        double durationMilliseconds = p.DurationTotalMilliseconds;
                        p.StartTime = new TimeCode(x.IdxLine.StartTime.TotalMilliseconds);
                        p.EndTime = new TimeCode(x.IdxLine.StartTime.TotalMilliseconds + durationMilliseconds);
                    }
                    _subtitle.Paragraphs.Add(p);
                }
            }

            return _subtitle;
        }

        public Bitmap subtitleBitmap(int index)
        {
            VobSubMergedPack pack = this.vobList[index];
            Bitmap bmp = pack.SubPicture.GetBitmap(_palette, Color.Transparent, Color.Wheat, Color.Black, Color.DarkGray, false);
            return bmp;
        }

    }
}
